﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using System.Diagnostics;
using System.Windows.Forms;



namespace WordXmlComparer
{
    public partial class Ribbon1 : OfficeRibbon
    {
        private string OriginalFile;
        private string ModifiedFile;

        public Ribbon1()
        {
            InitializeComponent();
            string tempFolder = GetTempFolder();
            OriginalFile = tempFolder + "\\Original.xml";
            ModifiedFile = tempFolder + "\\Modified.xml";
            
        }

        private void btnSaveOriginalXML_Click(object sender, RibbonControlEventArgs e)
        {
            Transform.ConvertToContentToXmlFile(Globals.ThisAddIn.Application.ActiveDocument.WordOpenXML, 
                OriginalFile);
                    
        }

        private void btnSaveModifiedXML_Click(object sender, RibbonControlEventArgs e)
        {
            Transform.ConvertToContentToXmlFile(Globals.ThisAddIn.Application.ActiveDocument.WordOpenXML,
                ModifiedFile);
        }

        private void btnCompareXML_Click(object sender, RibbonControlEventArgs e)
        {

            try
            {
                Process CompareExe = new Process();

                CompareExe.StartInfo.FileName = AppSettings.CompareExePath;
                CompareExe.StartInfo.Arguments = AppSettings.CompareExeArguments;
                if (AppSettings.CompareExeArguments.IndexOf("{0}") == -1)
                {
                    CompareExe.StartInfo.Arguments = AppSettings.CompareExeArguments;
                }
                else
                {
                    CompareExe.StartInfo.Arguments = string.Format(AppSettings.CompareExeArguments, OriginalFile, ModifiedFile);
                }
                CompareExe.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error while starting your external comparison tool. \n\n Verify the Word XML Compare options are correct. \n\n" + ex.Message, "Error");
            }
        }

        private void btnOptions_Click(object sender, RibbonControlEventArgs e)
        {
            frmOptions frm = new frmOptions();
            frm.Show();
        }

        /// <summary>
        /// Gets the temp folder including a trailing backslash. If the temp folder
        /// is not defined for the machine, it returns the root folder on the 'C' drive.
        /// </summary>
        /// <returns>temporary folder with a trailing backslash</returns>
        private string GetTempFolder()
        {
            string tempFolder = Environment.GetEnvironmentVariable("Tempxxx");

            if (tempFolder == null)
            {
                return "C:\\";
            }
            else
            {
                if (tempFolder.EndsWith("\\"))
                {
                    return tempFolder;
                }
                else
                {
                    return tempFolder + "\\";
                }
            }
        }
    }
}
